import { createStore } from 'vuex'
import {userLoginFrom} from "@/request/types/user";
import {getUserInfo, Login, loginOut} from "@/request/api/Login";
import {selectPostByLikeCountDesc, selectPostsByName, selectPostToDay} from "@/request/api/Posts";
import {BASE_GOODSIMG_URL} from "@/constant/module/file";

export default createStore({
    state: {
        //用户信息
        token: localStorage.getItem('token'),
        nickName: localStorage.getItem('nickName'),
        avatar: localStorage.getItem('avatar'),
        signatures: localStorage.getItem('signatures'),

        //帖子信息
        postList: [],
        postListDay: [],
        searchResult: []
    },
    mutations: {
        SET_TOKEN: (state, token: string) => {
            state.token = token
            localStorage.setItem('token', token)
        },
        SET_NICKNAME: (state, nickName: string) => {
            state.nickName = nickName
            localStorage.setItem('nickName', nickName)
        },
        SET_AVATAR: (state, avatar: string) => {
            state.avatar = avatar
            localStorage.setItem('avatar', avatar)
        },
        SET_SIGNATURES: (state, signatures: string) =>{
            state.signatures = signatures
            localStorage.setItem('signatures', signatures)
        },
        SET_POSTLIST: (state, postList: Array) => {
            state.postList = postList
            localStorage.setItem('postList', postList)
        },
        SET_POSTLISTBYDAY: (state, postList: Array) => {
            state.postListDay = postList
            localStorage.setItem('postListDay', postList)
        },
        SET_SEARCHRESULT: (state, searchResult: Array) => {
            state.searchResult = searchResult
        }


    },
    actions: {
        //登录
        Login({ commit }, userInfo: userLoginFrom) {
            return new Promise((resolve, reject) => {
                Login(userInfo).then((res: any) => {
                    if(res && res.code === 200) {
                        const data = res.data;
                        commit('SET_TOKEN', data.token)
                        resolve(res.code)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        //获取用户信息
        GetInfo({ commit }) {
                return new Promise((resolve, reject) => {
                    getUserInfo().then((res: any) => {
                        if (res && res.code === 200) {
                            const data = res.data;
                            console.log(data)
                            console.log(data.nickName)
                            const avatar = (data.avatar == "" || data.nickName == null) ? "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" : BASE_GOODSIMG_URL + data.avatar
                            const nickName = (data.nickName == "" || data.nickName == null) ? "1111" : data.nickName
                            console.log('11111111111' + avatar)
                            commit('SET_NICKNAME', nickName)
                            commit('SET_AVATAR', avatar)
                        }
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },

        // 退出系统
        loginOut({ commit }) {
          return new Promise((resolve, reject) => {
              loginOut().then((res) => {
              commit('SET_TOKEN', '')
              localStorage.clear()
              resolve(res.code)
            }).catch(error => {
              reject(error)
            })
          })
        },

        // 获取推荐帖子
        postByLike({ commit }) {
            return new Promise((resolve, reject) => {
                selectPostByLikeCountDesc().then((res) => {
                    console.log(res.data)
                    commit('SET_POSTLIST',res.data)
                    resolve(res.code)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 日榜
        postByDay({ commit }) {
            return new Promise((resolve, reject) => {
                selectPostToDay().then((res) => {
                    console.log(res.data)
                    commit('SET_POSTLISTBYDAY', res.data)
                    resolve(res.code)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        getPostsBySearchName({ commit }, searchName: any) {
            return new Promise((resolve, reject) => {
                selectPostsByName(searchName).then((res) => {
                    console.log(res.data)
                    commit('SET_SEARCHRESULT', res.data)
                    resolve(res.code)
                }).catch(error => {
                    reject(error)
                })
            })
        },




    },
    getters: {
    },
    modules: {
    }
})